package com.lego.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lego.bean.Seller;
import com.lego.bean.User;
import com.lego.bean.UsersPage;
import com.lego.service.SellerService;
import com.lego.service.UserService;
import com.lego.service.impl.SellerServiceImpl;
import com.lego.service.impl.UserServiceImpl;


@WebServlet("/querySellers.do")
public class QuerySellersServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		SellerService ss = new SellerServiceImpl();
		request.setCharacterEncoding("utf-8");
		String brandName = request.getParameter("brandName");
		int pageSize  = 5;
		int pageNum = 0;
		try {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		} catch (Exception e) {
			pageNum = 1;
		}
		int totalRows = ss.querySellerRow(brandName);
		List<Seller> list = ss.queryAllSeller(brandName,pageNum,pageSize);
		UsersPage usersPage = new UsersPage(list,pageSize,pageNum,totalRows,brandName);
		ObjectMapper om = new ObjectMapper();
		response.setContentType("application/json;charset=utf-8");
		String str = om.writeValueAsString(usersPage);
		System.out.println(str);
		response.getWriter().write(str);
	}

}
